package com.example.zhihuhelpapp.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.example.zhihuhelpapp.R;
import com.example.zhihuhelpapp.base.MyBaseRxActivity;
import com.example.zhihuhelpapp.bean.QuestionBean;
import com.example.zhihuhelpapp.bean.QuestionResultBean;
import com.example.zhihuhelpapp.mvp.QuestionListPresenter;
import com.example.zhihuhelpapp.mvp.contract.IQuestionListContract;
import com.example.zhihuhelpapp.utils.CoreLib;
import com.example.zhihuhelpapp.utils.DialogUtils;
import com.example.zhihuhelpapp.utils.UIUtils;

import java.util.List;

/**
 * 手动输入问题或回答链接
 */
public class InputQuestionOrAnswerUrlActivity extends MyBaseRxActivity<QuestionListPresenter> implements IQuestionListContract.IView {
    private EditText et_input;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_input_question_or_answer_url;
    }

    @Override
    protected void initView() {
        showToolBar(true, getString(R.string.page_input_by_user));
        et_input = findViewById(R.id.et_input);
    }

    @Override
    protected void initData() {
        super.initData();

        final String pastContent = UIUtils.getPastContent(this);
        if (!TextUtils.isEmpty(pastContent) && CoreLib.isNeedUrl(pastContent)) {
            DialogUtils.showDialog(this, "提示", "检测到链接：" + pastContent + "，是否粘贴？", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    et_input.setText(pastContent);

                }
            });
        }
    }

    @Override
    protected QuestionListPresenter createPresenter() {
        return new QuestionListPresenter();
    }

    public void confirm(View view) {
        String content = et_input.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            UIUtils.showToast(R.string.app_input_by_user_hint);
            return;
        }
        if (CoreLib.isAnswer(content)) {
            String answerId = CoreLib.getQuestionOrAnswerId(content);
            mPresenter.getAnswerDetail(answerId);
        } else if (CoreLib.isQuestion(content)) {
            String questionId = CoreLib.getQuestionOrAnswerId(content);
            mPresenter.getQuestionDetail(questionId, 0);
        }
    }

    @Override
    public void onSaveQuestionSuccess() {
        UIUtils.showToast(R.string.save_success);
        finish();
        startActivity(new Intent(this,QuestionListActivity.class));
    }

    @Override
    public void onDelQuestionSuccess() {

    }

    @Override
    public void onGetLocalQuestionList(List<QuestionBean> questionBeanList) {

    }

    @Override
    public void onGetQuestionDetail(QuestionResultBean resultBean) {
        if (resultBean != null) {
            List<QuestionResultBean.DataBean> dataList = resultBean.getData();
            QuestionBean questionBean = new QuestionBean();
            questionBean.setQuestionAnswerCount(resultBean.getPaging().getTotals());
            if (dataList != null && dataList.size() > 0) {
                QuestionResultBean.DataBean dataBean = dataList.get(0);
                QuestionResultBean.DataBean.QuestionBean question = dataBean.getQuestion();
                questionBean.setQuestionId(String.valueOf(question.getId()));
                questionBean.setQuestionTitle(question.getTitle());
            }
            questionBean.setUpdateTime(System.currentTimeMillis());
            mPresenter.saveQuestionToLocal(questionBean);
        }
    }
}
